package com.hp.common.logger;

import com.hp.common.config.GlobalProperties;
import com.hp.common.tool.StringTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/** 简单日志输出 */
@Component
@Order(-1)
public class DefaultLoggerStoreImpl implements ILoggerStore {

  private static final Logger LOGGER = LoggerFactory.getLogger(DefaultLoggerStoreImpl.class);
  @Autowired private GlobalProperties properties;

  @Override
  public int save(LoggerModel model) {
    if (properties.isOpenSensitive()) {
      properties
          .getSensitiveKeys()
          .forEach(
              key -> {
                if (StringTool.isNotEmpty(model.getBody())) {
                  model.setBody(model.getBody().replaceAll("(" + key + "=)([^&]+)", "$1*****"));
                  model.setBody(
                      model.getBody().replaceAll("(\"" + key + "\":\\s*\")([^\"]+)", "$1*****"));
                }
              });
    }
    LOGGER.info(
        "{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}",
        model.getTrackId(),
        model.getUri(),
        model.getQueryString(),
        model.getMethod(),
        model.getDescription(),
        model.getIp(),
        model.getBody(),
        model.getToken(),
        model.getUserId(),
        model.getUserName(),
        model.getExt() != null ? model.getExt().toString() : "",
        model.getReturnData(),
        model.getStartTime(),
        model.getEndTime());
    return 1;
  }
}
